﻿using Helper;
using Helper.Models;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Input;

namespace Starehe.ViewModels
{
    public class EnterExamResultsVM : ViewModelBase
    {
        ExamResultModel newResult;
        ExamResultSubjectModel newSubjectResult;
        ObservableCollection<ExamModel> allExams;
        ObservableCollection<ExamResultSubjectModel> allSubjects;
        public EnterExamResultsVM()
        {
            InitVars();
            CreateCommands();

        }

        protected override void InitVars()
        {
            Title = "ENTER EXAM RESULTS";
            NewResult = new ExamResultModel();
            SelectedSubject = new ExamResultSubjectModel();
            SelectedStudentID = 0;
            SelectedExamID = 0;
            AllExams = new ObservableCollection<ExamModel>();
            AllSubjects = new ObservableCollection<ExamResultSubjectModel>();
        }

        protected override void CreateCommands()
        {
            AddSubjectResultCommand = new RelayCommand(o =>
            {
                newResult.Entries.Add(newSubjectResult);
                SelectedSubject = new ExamResultSubjectModel();
                SelectedSubject = null;
            },
            o =>
            {
                return newSubjectResult != null && newSubjectResult.SubjectID > 0 &&
                    newSubjectResult.Score > 0 && !SubjectExists(newSubjectResult.SubjectID);
            });

            SaveCommand = new RelayCommand(async o =>
            {
                newResult.ExamResultID = await DataAccessHelper.GetNewIDAsync("School.ExamResultHeader");
                if (newResult.ExamResultID<=0)
                    return;
                bool succ = await DataAccess.SaveNewExamResultAsync(newResult);
                if (succ)
                    Reset();
               
            }, o =>
            {
                return true;
            });
        }

        private bool SubjectExists(int ID)
        {
            bool exists = false;
            foreach (SubjectModel s in newResult.Entries)
                if (s.SubjectID == ID)
                {
                    exists = true;
                    break;
                }
            return exists;
        }


        public int SelectedStudentID
        {
            get { return newResult.StudentID; }

            set
            {
                if (value != newResult.StudentID)
                {
                    newResult.Reset();
                    newResult.StudentID = value;
                    NotifyPropertyChanged("SelectedStudentID");
                    if (newResult.StudentID > 0)
                        RefreshExamEntries();
                    else
                        Reset();
                }
            }
        }

        private async void RefreshExamEntries()
        {
            int classID = await DataAccess.GetClassIDFromStudentID(newResult.StudentID);
            AllExams = await DataAccess.GetExamsByClass(classID);            
        }

        public int SelectedExamID
        {
            get { return newResult.ExamID; }

            set
            {
                if (value != newResult.ExamID)
                {
                    newResult.ExamID = value;
                    NotifyPropertyChanged("SelectedExamID");
                    if (newResult.ExamID > 0)
                        RefreshSubjectEntries();
                    else
                        AllSubjects.Clear();
                }
            }
        }

        private async void RefreshSubjectEntries()
        {
            AllSubjects.Clear();
            var temp = (await DataAccess.GetExamAsync(newResult.ExamID)).Entries;
            foreach (SubjectModel sm in temp)
                AllSubjects.Add(new ExamResultSubjectModel(sm));
            newResult.Entries = (await DataAccess.GetExamResultAync(newResult.StudentID, newResult.ExamID)).Entries;
        }

        public ExamResultSubjectModel SelectedSubject
        {
            get { return newSubjectResult; }

            set
            {
                if (value != newSubjectResult)
                {
                    newSubjectResult = value;
                    NotifyPropertyChanged("SelectedSubject");
                }
            }
        }

        public ObservableCollection<ExamModel> AllExams
        {
            get { return this.allExams; }

            private set
            {
                if (value != this.allExams)
                {
                    this.allExams = value;
                    NotifyPropertyChanged("AllExams");
                }
            }
        }

        public ObservableCollection<ExamResultSubjectModel> AllSubjects
        {
            get { return this.allSubjects; }

            private set
            {
                if (value != this.allSubjects)
                {
                    this.allSubjects = value;
                    NotifyPropertyChanged("AllSubjects");
                }
            }
        }
        
        public ExamResultModel NewResult
        {
            get { return this.newResult; }

            private set
            {
                if (value != this.newResult)
                {
                    this.newResult = value;
                    NotifyPropertyChanged("NewResult");
                }
            }
        }

        public ICommand AddSubjectResultCommand
        {
            get;
            private set;
        }

        public ICommand SaveCommand
        {
            get;
            private set;
        }

        public override void Reset()
        {
            NewResult = new ExamResultModel();
            SelectedSubject = new ExamResultSubjectModel();
            SelectedStudentID = 0;
            SelectedExamID = 0;
            AllExams = new ObservableCollection<ExamModel>();
            AllSubjects = new ObservableCollection<ExamResultSubjectModel>();
        }
    }
}
